package com.css.apps.base.post.service;

import java.util.List;

import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.role.model.SRole;
import com.css.db.query.QueryCache;
import com.css.util.StringHelper;

public class SPostItem {

	@SuppressWarnings("unchecked")
	public static boolean getSPostList() {
		QueryCache qc = new QueryCache("select a.uuid from SPost a");
		List<String> li = qc.list();
		if (li != null && li.size() > 0) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public static List<SRole> getRoleList(String postId) {
		if (StringHelper.isEmpty(postId)) {
			return null;
		}
		QueryCache qc = new QueryCache("select a.roleId from SPostRole a where a.postId=:postId").setParameter("postId",
				postId.trim());
		if (qc.list() != null && qc.list().size() != 0) {
			return QueryCache.idToObj(SRole.class, qc.list());
		} else {
			return null;
		}
	}

	// 2015/07/15钱正金
	@SuppressWarnings("unchecked")
	public static List<String> getRoleIdList(String postId) {
		if (StringHelper.isEmpty(postId)) {
			return null;
		}
		QueryCache qc = new QueryCache("select a.roleId from SPostRole a where a.postId=:postId").setParameter("postId",
				postId.trim());
		if (qc.list() != null && qc.list().size() != 0) {
			return qc.list();
		} else {
			return null;
		}
	}

	public static Number getRoleListCount(String postId) {
		if (StringHelper.isEmpty(postId)) {
			return null;
		}
		QueryCache qc = new QueryCache("select count(a.roleId) from SPostRole a where a.postId=:postId")
				.setParameter("postId", postId.trim());
		Number num = (Number) qc.uniqueResult();
		return num;
	}

	@SuppressWarnings("unchecked")
	public static String getOrgPostName(String orgid) {
		SOrg orgitem = QueryCache.get(SOrg.class, orgid);
		List<SPost> postList = orgitem.getPostList().getList();
		String postName = "";
		if (postList != null) {
			for (SPost item : postList) {
				postName += item.getName();
				postName += " ; ";
			}
			if (postName.length() > 2) {
				String post = postName.substring(0, postName.length() - 2);
				return post;
			} else {
				return postName;
			}
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static java.util.List<String> queryOrgListByPost(String postId) {
		List<String> idlist = new QueryCache("select a.uuid from SOrg a ").list();
		List<String> uplist = new java.util.LinkedList<String>();
		for (String orgId : idlist) {
			SOrg org = QueryCache.get(SOrg.class, orgId);
			if (org != null && org.getPostList().getListById().contains(postId)) {
				uplist.add(orgId.trim());
			}
		}
		return uplist;
	}

	public static String getPostRoleName(String postId) {
		String roleName = "";
		List<SRole> postRoleList = SPostItem.getRoleList(postId);
		for (SRole role : postRoleList) {
			roleName += role.getName();
			roleName += " ; ";
		}
		if (roleName.length() > 2) {
			String post = roleName.substring(0, roleName.length() - 2);
			return post;
		} else {
			return roleName;
		}

	}

}
